﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Business.Pur;
using ESOL.Common;
using ESOL.Common.Entity;
using ESOL.WinUI.Hr;

namespace ESOL.WinUI.Pur
{
    public partial class frmManagePurchaseApplication : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private PurApplicationEntity mSelectedPurApplication;
        #endregion

        #region Constructor
        public frmManagePurchaseApplication()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbIsProductPurchase, typeof(PurchaseCategory), typeof(int), true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbPurchaseType, "PURTYPE", true);

            UICommon.UICommonFunctions.BindGridComboboxByEnum(colIS_PRODUCT_PURCHASE, typeof(PurchaseCategory), typeof(int), false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colPURCHASE_TYPE, "PURTYPE", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colSTATUS, "STATUS", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colUNIT, "UNIT", true);

            cmbStatus.SelectedValue = "STATUS02";
        }
        private string GetCondition()
        {
            string condition = "";
            if (string.IsNullOrEmpty(txtApplicationCode.Text) == false)
            {
                condition += " AND PA.APPLICATION_CODE LIKE '%" + txtApplicationCode.Text + "%'";
            }
            if (dtpApplicationDate.EditValue!=null)
            {
                condition += " AND PA.APPLICATION_DATE >= CONVERT(DATETIME,'" + ((DateTime)dtpApplicationDate.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (dtpApplicationDateTo.EditValue!=null)
            {
                condition += " AND PA.APPLICATION_DATE <= CONVERT(DATETIME,'" + ((DateTime)dtpApplicationDateTo.EditValue).ToString("yyyy-MM-dd") + "')";
            }
            if (txtApplicationPerson.Tag != null && txtApplicationPerson.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity entity = (MstPersonEntity)txtApplicationPerson.Tag;
                condition += " AND PA.APPLICATION_PERSON = '" + entity.PersonId + "' ";
            }
            else
            {
                if (!mCanQueryOtherPerson)
                {
                    condition += " AND PA.APPLICATION_PERSON = '" + CommonParameter.CurrentUser.PersonId + "' ";
                }
            }
            if (string.IsNullOrEmpty(cmbIsProductPurchase.Text) == false && cmbIsProductPurchase.SelectedValue != null)
            {
                condition += " AND PA.IS_PRODUCT_PURCHASE = '" + cmbIsProductPurchase.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbPurchaseType.Text) == false && cmbPurchaseType.SelectedValue != null)
            {
                condition += " AND PA.PURCHASE_TYPE = '" + cmbPurchaseType.SelectedValue.ToString() + "'";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND PA.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "PA.APPLICATION_CODE DESC";
            DataTable dt = PurApplicationBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }

        private void BindDetailGrid(string appId)
        {
            if (string.IsNullOrEmpty(appId))
            {
                gvDetail.DataSource = null;
                return;
            }
            DataSet ds = PurApplicationItemBs.GetDataSetByApplicationID(appId);
            if (ds == null || ds.Tables.Count == 0)
            {
                gvDetail.DataSource = null;
            }
            gvDetail.DataSource = ds.Tables[0];
        }

        private void ResetButtonsEnable()
        {
            btnModify.Enabled = false;
            btnDelete.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            if (mSelectedPurApplication == null)
            {
                return;
            }
            if (mSelectedPurApplication.Status == NormalStatus.STATUS01.ToString()
                || mSelectedPurApplication.Status == NormalStatus.STATUS02.ToString()
                || mSelectedPurApplication.Status == NormalStatus.STATUS04.ToString())
            {
                btnModify.Enabled = true;
                btnDelete.Enabled = true;
                btnApprove.Enabled = true;
            }
            else if (mSelectedPurApplication.Status == NormalStatus.STATUS03.ToString())
            {
            }
            else if (mSelectedPurApplication.Status == NormalStatus.STATUS05.ToString())
            {
            }
        }
        #endregion

        #region Event
        protected override void OnShown(EventArgs e)
        {
            mCanQueryOtherPerson = UICommon.UICommonFunctions.CheckPersonRight(RoleCategory.采购);
            BindComboBox();
            BindDataGrid();
            base.OnShown(e);
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPurApplication = new PurApplicationEntity(dr);
            BindDetailGrid(mSelectedPurApplication.ApplicationId);
            ResetButtonsEnable();
        }

        protected override void Click_View()
        {
            if (mSelectedPurApplication == null)
            {
                MessageBox.Show("请选择一个采购申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPurchaseApplication frm = new frmMaintainPurchaseApplication();
            frm.ThisActionType = ActionType.View;
            frm.SelectedPurApplication = mSelectedPurApplication;
            frm.ShowDialog();
        }

        protected override void Click_Delete()
        {
            if (mSelectedPurApplication == null)
            {
                MessageBox.Show("请选择一个采购申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否删除选中采购申请单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedPurApplication.Status = NormalStatus.STATUS05.ToString();
                mSelectedPurApplication.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedPurApplication.LastModifiedDate = DateTime.Now;
                bool successful = PurApplicationBs.UpdatePurApplication(mSelectedPurApplication);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Submit()
        {
            if (mSelectedPurApplication == null)
            {
                MessageBox.Show("请选择一个采购申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否送审选中采购申请单？", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mSelectedPurApplication.Status = NormalStatus.STATUS02.ToString();
                mSelectedPurApplication.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mSelectedPurApplication.LastModifiedDate = DateTime.Now;
                bool successful = PurApplicationBs.UpdatePurApplication(mSelectedPurApplication);
                if (successful)
                {
                    MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        protected override void Click_Approve()
        {
            if (mSelectedPurApplication == null)
            {
                MessageBox.Show("请选择一个采购申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPurchaseApplication frm = new frmMaintainPurchaseApplication();
            frm.ThisActionType = ActionType.Approve;
            frm.SelectedPurApplication = mSelectedPurApplication;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_New()
        {
            frmMaintainPurchaseApplication frm = new frmMaintainPurchaseApplication();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Modify()
        {
            if (mSelectedPurApplication == null)
            {
                MessageBox.Show("请选择一个采购申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainPurchaseApplication frm = new frmMaintainPurchaseApplication();
            frm.ThisActionType = ActionType.Modify;
            frm.SelectedPurApplication = mSelectedPurApplication;
            DialogResult dr = frm.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        private void txtApplicationPerson_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtApplicationPerson.Text = frm.SelectedPerson.NameCn;
                txtApplicationPerson.Tag = frm.SelectedPerson;
            }
            else
            {
                txtApplicationPerson.Text = "";
                txtApplicationPerson.Tag = null;
            }
        }
        #endregion

    }
}
